package handler

import (
	"code.cloudfoundry.org/lager"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io"

	"io/ioutil"
	"net/http"
	"os"
)

const JSON = "application/json"

var log lager.Logger
var ssoHost, dfHost string
func check(e error) {
	if e != nil {
		panic(e)
	}
}
/**
* 判断文件是否存在 存在返回 true 不存在返回false
 */
func checkFileIsExist(filename string) (bool) {
	var exist = true;
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		exist = false;
	}
	return exist;
}
func init() {
	//dfHost = getENV("DFHOST")
	log = lager.NewLogger("Token_Proxy")
	log.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
}

type Postbody struct {
	Cell string `json:"cell"`
	Element []string `json:"element"`
	LOAD []string `json:"load"`
}
//type Postbody struct {
//	N string `json:"n"`
//	M string `json:"m"`
//	H string `json:"H"`
//	L string `json:"L"`
//	Attr []Point `json:"attr"`
//	LOAD []Load `json:"load"`
//}
//type Point struct {
//	SOURCE Pointtype `json:"source"`
//	TARGET Pointtype `json:"target"`
//	MATERIAL string `json:"material"`
//	SECTION string `json:"section"`
//	SIZE Sizetype `json:"size"`
//}
//type Pointtype struct {
//	POINT string `json:"point"`
//	COORDINATE Coordinate `json:"coordinate"`
//}
//type Coordinate struct {
//	X int `json:"x"`
//	Y int `json:"y"`
//}
//type Sizetype struct {
//	T3 string `json:"t3"`
//	Tw string `json:"tw"`
//	T2 string `json:"t2,omitempty"`
//	TF string `json:"tf,omitempty"`
//}
//type Load struct {
//	SOURCE Pointtype `json:"source"`
//	TARGET Pointtype `json:"target"`
//	GERINO string `json:"region"`
//	INTRODUCE string `json:"introduce"`
//	DEADLOAD string `json:"deadload"`
//	LIVELOAD string `json:"liveload"`
//}


func Cluster(c *gin.Context) {
	//var wireteString = "测试n"
	var filename = "./outputfile.txt";
	var f *os.File
	var err1 error;
	//token := getToken(c)
	rBody, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		log.Error("CreateBC Read Request.Body error", err)
	}
	var postbody Postbody

	_ = json.Unmarshal(rBody, &postbody)
	fmt.Printf("%s\n",rBody)

	if checkFileIsExist(filename) { //如果文件存在
		err1=os.Truncate(filename, 0)
		f, err1 = os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC, 0600) //打开文件
		defer f.Close()

		fmt.Println("文件存在");
	}else {
		f, err1 = os.Create(filename) //创建文件
		fmt.Println("文件不存在");
	}
	check(err1)
	n, err1 := io.WriteString(f, postbody.Cell) //写入文件(字符串)
	for _, v := range postbody.Element {
		_, err1 := io.WriteString(f, v)
		check(err1)
	}
	for _, v := range postbody.LOAD {
		_, err1 := io.WriteString(f, v)
		check(err1)
	}
	check(err1)
	fmt.Printf("写入 %d 个字节n", n);



	//fmt.Println("----- ",postbody)
	//users :=c.Request.Header["Authorization"][0]

	//errorRsp := ErrorResponse{}

	//result := ""

	//dfRsp, err := trRequest("GET", "https://"+dfHost+"/oauth/authorize?client_id=openshift-challenging-client&response_type=token", user)

	//if err != nil {
	//	log.Error("GetToken request fail error", err)
	//	errorRsp.Description = "GetToken request fail error"
	//	errorRsp.Error = err.Error()
	//	c.JSON(500, errorRsp)
	//	return
	//}
	//
	//if value, ok := dfRsp.Header["Location"]; ok {
	//	result = getDFtoken(value[0])
	//} else {
	//	log.Debug("GetToken respose header fail error")
	//	errorRsp.Description = "GetToken respose header fail error"
	//	errorRsp.Error = err.Error()
	//	c.JSON(500, errorRsp)
	//	return
	//}

	//if len(result) == 0 {
	//	log.Debug("GetToken respose header[location]  fail error")
	//	errorRsp.Description = "GetToken respose header[location]  fail error"
	//	errorRsp.Error = err.Error()
	//	c.JSON(500, errorRsp)
	//	return
	//}

	//rsp := RstResponse{}
	//rsp.User = "admin"
	////rsp.Token = result
	//fmt.Printf("%#v\n",rsp)
	c.JSON(http.StatusOK, postbody)
	return

}
